package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.model.BeanModelMasterBb;
import id.net.iconpln.bb.client.model.BeanModelPengendalianBb;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;
import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.service.PengendalianRPCService;
import id.net.iconpln.bb.client.service.PengendalianRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.MultiField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.CellSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbPasokan extends LayoutContainer {
	private final PengendalianRPCServiceAsync service = GWT.create(PengendalianRPCService.class);
	private final MasterRPCServiceAsync masterService = GWT.create(MasterRPCService.class);
	private FormData formData;	
	private FormBinding formBindings;
	private Grid gridPltu,gridRencKirim;
	private ListStore storePltu,storePemasok,storeJnsKontrak;
	private GroupingStore storeRencKirim;
	private Button bSubmit;
	private TextField noPjbb;
	private ComboBox cboPemasok,cboJnsKontrak;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    String mdl = Registry.get("isModule");
	    initStore();
	    
	    DateTimeFormat dt = DateTimeFormat.getFormat("dd/MM/YYYY");
	    formData = new FormData("-20");
	    
	    ContentPanel cp = new ContentPanel();    
	    cp.setHeaderVisible(false);
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
//	    
	    gridPltu = createGridPltu();

	    ContentPanel pPltu = new ContentPanel();
	    pPltu.setHeaderVisible(false);
	    pPltu.setLayout(new FitLayout());
	    pPltu.add(gridPltu);
//	    
	    ToolBar tbPltu = new ToolBar();
	    tbPltu.add(new LabelToolItem("Data Pembangkit"));
	    tbPltu.add(new SeparatorToolItem());
	    pPltu.setTopComponent(tbPltu);
//	    
	    getServiceInfo("master_pltu","LISTPASOKAN");
	   	    

	    gridRencKirim = createGridRencPengiriman();
	    
	    ContentPanel pDelimeter = new ContentPanel();
	    pDelimeter.setWidth(10);
	    pDelimeter.setHeaderVisible(false);
	    pDelimeter.setLayout(new FitLayout());
	    
	    ToolBar tbKontrak = new ToolBar();
	    tbKontrak.add(new LabelToolItem("Data Pasokan"));
	    tbKontrak.add(new SeparatorToolItem());

	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);
	    p.setLayout(new RowLayout(Orientation.VERTICAL));	    	    
	    
//	    if (mdl.equals("PENGADAAN")){
//	    	p.add(pPltu, new RowData(.99, .4));
//	    	p.add(pFormKontrak, new RowData(.99, .6));	    	
//	    }else{
	    	p.add(pPltu, new RowData(.99, 1));
//	    }

	    p.setTopComponent(tbPltu);

	    ContentPanel pKirim = new ContentPanel();
	    pKirim.setHeaderVisible(false);
	    pKirim.setLayout(new RowLayout(Orientation.HORIZONTAL));
	    pKirim.add(gridRencKirim,new RowData(.99, 1));
	    pKirim.setTopComponent(tbKontrak);
	    
	    cp.add(p, new RowData(.4, 1));
//	    cp.add(pDelimeter, new FitData(5));
	    cp.add(pKirim, new RowData(.6, 1));
	    
	    
	    add(cp, new FitData(5)); 
	  }  
	  
	        
	    private void initStore(){
	      	storePltu = new ListStore();	
	      	storeRencKirim = new GroupingStore();
	     }

	    private Grid createGridPltu(){
    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

    ColumnConfig column = new ColumnConfig();	    	     
    
    column = new ColumnConfig();  
    column.setId("id_pltu");  
    column.setHidden(true);
    column.setWidth(1);    
    configs.add(column);  


    column = new ColumnConfig();  
    column.setId("nama_pembangkit");  
    column.setHeader("Nama");    
    column.setWidth(200);    
    configs.add(column);  

    column = new ColumnConfig();  
    column.setId("jml_pemasok");  
    column.setHeader("Pemasok");  
    column.setWidth(90);  
    configs.add(column);  	  

    storePltu.setMonitorChanges(true);
    
    ColumnModel cm = new ColumnModel(configs);  
  
    gridPltu = new Grid(storePltu, cm);
    gridPltu.setLoadMask(true);  
    gridPltu.mask("load Data ...","x-mask-loading");
    gridPltu.getView().setEmptyText("Data Empty");  
    gridPltu.setBorders(false);  
//    grid.setAutoExpandColumn("pemasok");  
    gridPltu.setBorders(true);  
    gridPltu.addListener(Events.CellClick, new Listener<BaseEvent>() {            
        public void handleEvent(BaseEvent ge) {
        	try{        		
        		Registry.register("id_pltu",gridPltu.getSelectionModel().getSelectedItem().get("id_pltu").toString());
        		gridRencKirim.mask("load Data ....","x-mask-loading");
        		getServiceInfo("master_kirim","LISTPASOKANBYPEMASOK");
//        		getServiceInfo("master_pemasok","PEMASOK_BY_PLTU");
        		
        	}catch (Exception e) {
				// TODO: handle exception
        		e.getMessage();
			}
        	
        }
	});

		return gridPltu;
	
	} 
private void getServiceInfo(String ctrl,String kode){
	Map mapParam = new FastMap();
//	if (ctrl.equals("master_pemasok")){
//		mapParam.put("id_pltu",Registry.get("id_pltu"));
//				masterService.getMaster(kode,mapParam,new AsyncCallback<List<Map>>() {
//						public void onFailure(Throwable caught) {
//							GWT.log(caught.getMessage(), null);
//						}
//						public void onSuccess(List<Map> result) {
//					    	storePemasok.removeAll();
//					    	storePemasok.add(ClientModule.setListBaseModelFromListMap(result));
////					    	storePemasok.unmask();
//					}
//				});
//	}else if (ctrl.equals("master_jnskontrak")){ 			
//		masterService.getMaster(kode,mapParam,new AsyncCallback<List<Map>>() {
//			public void onFailure(Throwable caught) {
//				GWT.log(caught.getMessage(), null);
//			}
//			public void onSuccess(List<Map> result) {
//				storeJnsKontrak.removeAll();
//				storeJnsKontrak.add(ClientModule.setListBaseModelFromListMap(result));
////		    	storePemasok.unmask();
//		}
//	});
//	}else 
   if (ctrl.equals("master_pltu")) {
		
	service.getListPengiriman(kode,mapParam,new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}
			public void onSuccess(List<Map> result) {
		    	storePltu.removeAll();
		    	storePltu.add(ClientModule.setListBaseModelFromListMap(result));
		    	gridPltu.unmask();
		}
	});
	}else if (ctrl.equals("master_kirim")) {
		mapParam.put("id_pltu",Registry.get("id_pltu"));
		service.getListPasokan(kode,mapParam,new AsyncCallback<List<Map>>() {
				public void onFailure(Throwable caught) {
					GWT.log(caught.getMessage(), null);
				}
				public void onSuccess(List<Map> result) {
			    	storeRencKirim.removeAll();
			    	storeRencKirim.add(ClientModule.setListBaseModelFromListMap(result));
			    	gridRencKirim.unmask();
			}
		});
	}
}

private Grid createGridRencPengiriman(){
	
	 storeRencKirim.groupBy("pemasok");  
	 List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

	    ColumnConfig column = new ColumnConfig();	    	     
	  
	    column = new ColumnConfig();  
	    column.setId("pemasok");  
	    column.setHeader("Pemasok");  
	    column.setWidth(150);  
	    configs.add(column);  	  

	    column = new ColumnConfig();  
	    column.setId("bulan");  
	    column.setHeader("Bulan");  
	    column.setWidth(150);  
	    configs.add(column);  	  
	    
	    
	    column = new ColumnConfig();  
	    column.setId("volume");  
	    column.setHeader("Volume");  
	    column.setAlignment(HorizontalAlignment.RIGHT);
	    column.setWidth(150);  
	    configs.add(column);  	  
	    
	    
	    storePltu.setMonitorChanges(true);
	    
	    final ColumnModel cm = new ColumnModel(configs);  
	 
	    GroupingView view = new GroupingView();  
	    view.setShowGroupedColumn(false);  
//	    view.setForceFit(true);  
	    view.setGroupRenderer(new GridGroupRenderer() {  
		@Override
		public String render(GroupColumnData data) {
			// TODO Auto-generated method stub
			  String f = cm.getColumnById(data.field).getHeader();  
		        String l = data.models.size() == 1 ? "Pemasok" : "Pemasok";  
		        return f + ": " + data.group ; //+ " (" + data.models.size() + " " + l + ")";
		}  
	    });
	    
	    gridRencKirim = new Grid(storeRencKirim, cm);
	    gridRencKirim.setView(view);  
//	    gridKontrak.setLoadMask(true);  
//	    gridKontrak.mask("load Data ...","x-mask-loading");
	    gridRencKirim.getView().setEmptyText("Data Empty");  
	    gridRencKirim.setBorders(false);  
//	    grid.setAutoExpandColumn("pemasok");  
	    gridRencKirim.setBorders(true);  
	  

			return gridRencKirim;
	}

}


